<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="抄表本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bookId">
              <j-search-select-tag v-model="model.bookId" dict="mws_t_mr_books,name,id" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readMonth">
              <a-input v-model="model.readMonth" disabled  ></a-input>              
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <j-popup
                v-model="model.cusName"
                field="cusName"
                org-fields="id,name"
                dest-fields="customerId,cusName"
                code="arc_customer"
                :multi="true"
                @input="popupCallback"
                disabled
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="起度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginScale">
              <a-input-number v-model="model.beginScale" disabled style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="止度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endScale">
              <a-input-number v-model="model.endScale"  @blur="endScalChange" placeholder="请输入止度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="用量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" @blur="amountChange" style="width: 100%"/>
            </a-form-model-item>
          </a-col>          
          <a-col :span="12">
            <a-form-model-item label="用水状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waterUsedType">
              <j-dict-select-tag type="list" @change="waterUTChange" v-model="model.waterUsedType" dictCode="water_used_type" placeholder="请选择用水状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="抄表员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readerId">
              <j-select-user-by-dep v-model="model.readerId" disabled />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="录入方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waterReadInput">
              <j-dict-select-tag type="list" v-model="model.waterReadInput" dictCode="water_read_input" placeholder="请选择录入方式" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readTime">
              <j-date  disabled  v-model="model.readTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入说明"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MrRecordsForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           beginScale: [
              { required: true, message: '请输入起度!'},
           ],
           endScale: [
              { required: true, message: '请输入止度!'},
           ],
           amount: [
              { required: true, message: '请输入用量!'},
           ],
           waterUsedType: [
              { required: true, message: '请输入用水状态!'},
           ],
           readerId: [
              { required: true, message: '请输入抄表员!'},
           ],
           waterReadInput: [
              { required: true, message: '请输入录入方式!'},
           ],
        },
        url: {
          add: "/mr/mrRecords/add",
          edit: "/mr/mrRecords/changeEdit",
          queryById: "/mr/mrRecords/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      endScalChange(){
        this.amountmeasure();
        if(this.model.waterUsedType != 1 ){
          this.model.amount='';
        }
      },
      amountChange(){
        this.amountmeasure(); 
      },
      waterUTChange(){
       this.amountmeasure(); 
       if(this.model.waterUsedType != 1 ){
          this.model.amount='';
        }
      }, 
      amountmeasure(){
        let beginS=this.model.beginScale;
        let endS=this.model.endScale;        
        let a=endS-beginS;
        if(this.model.waterUsedType == 1 ){
          this.model.amount=a;
        }
      },  
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>